package com.yequan.drawerlayoutdemo;


import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.yequan.drawerlayoutdemo.skin.SkinManager;
import com.yequan.drawerlayoutdemo.skin.base.BaseSkinActivity;
import com.yequan.drawerlayoutdemo.skin.callback.ISkinChangingCallback;
import com.yequan.drawerlayoutdemo.skin.config.Const;

import java.io.File;


/**
 * A simple {@link Fragment} subclass.
 */
public class MenuItemFragment extends Fragment implements View.OnClickListener {

    private Activity mContext;
    private View mHZW;
    private View mHYRZ;
    private View mRestore;
    private View mPlugin;
    private String[] mPermissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    public MenuItemFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = this.getActivity();
        return inflater.inflate(R.layout.layout_menu, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initEvents();
    }

    private void initEvents() {
        mHZW.setOnClickListener(this);
        mHYRZ.setOnClickListener(this);
        mRestore.setOnClickListener(this);
        mPlugin.setOnClickListener(this);
    }

    private void initView(View view) {
        mHZW = view.findViewById(R.id.id_rl_innerchange01);
        mHYRZ = view.findViewById(R.id.id_rl_innerchange02);
        mRestore = view.findViewById(R.id.id_restore);
        mPlugin = view.findViewById(R.id.id_changeskin);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_rl_innerchange01:
                SkinManager.getInstance().changeSkin("haizeiwang");
                break;
            case R.id.id_rl_innerchange02:
                SkinManager.getInstance().changeSkin("huoyingrenzhe");
                break;
            case R.id.id_changeskin:
                if (BaseSkinActivity.hasPermission(mContext, mPermissions[0])) {
                    doSDCardPermission();
                } else {
                    BaseSkinActivity.requestPermission(mContext, Const.WRITE_EXTERNAL_CODE, mPermissions[0]);
                }
                break;
            case R.id.id_restore:
                SkinManager.getInstance().removeAnySkin();
                break;
        }
    }

    private String mSkinPluginPath = Environment.getExternalStorageDirectory().getPath()
            + File.separator + "plugins" + File.separator + "skin_plugin.apk";
    private String mSkinPluginPkg = "com.yequan.skin_plugin";

    public void doSDCardPermission() {
        SkinManager.getInstance().changeSkin(mSkinPluginPath, mSkinPluginPkg, new ISkinChangingCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Exception e) {

            }

            @Override
            public void onComplete() {
                Toast.makeText(mContext, "换肤成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
